package cn.kent;

import cn.kent.entity.Employee;
import org.junit.Test;

import java.util.Arrays;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 构造器引用 & 数组引用
 *  {@link Supplier#get()}
 *
 *  构造器引用：
 *          和方法引用类似，函数时接口的抽象方法的形参列表和构造器的形参列表一致
 *
 *  数组引用：
 *      数组可以看作是一个特殊的类，写法和构造器引用一样。
 */
public class ConstructRefTest {

    /**
     * 无参构造函数引用
     */
    @Test
    public  void test(){
        Supplier<Employee> e = Employee::new;
        final Employee employee = e.get();
        System.out.println(employee.toString());
    }

    /**
     * 一个参数构造函数
     */
    @Test
    public void test2(){
        Function<Integer, Employee> func = id -> new Employee(id);
        final Employee e = func.apply(1);
        System.out.println(e.toString());
    }

    /**
     * 多参构造函数
     */
    @Test
    public void test3(){
        BiFunction<Integer, String, Employee> biFunc = (id, name) -> new Employee(id, name);
        final Employee e = biFunc.apply(1, "张三");
        System.out.println(e.toString());
    }

    /**
     * 数组引用
     */
    @Test
    public void test4(){
        Function<Integer, String[]> func = length -> new String[length];
        final String[] strs = func.apply(5);
        System.out.println(Arrays.toString(strs));

        Function<Integer, String[]> func2 = String[]::new;
        final String[] strs1 = func2.apply(6);
        System.out.println(Arrays.toString(strs1));
    }
}
